package com.test.list.activity;

/** Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;

import com.test.list.R;
import com.test.list.adapter.AmazingLazyAdapter;
import com.test.list.classes.ViewableList;
import com.test.list.contacts.ContactMngr;
import com.test.list.interfaces.Viewable;
import com.test.list.simple.Controller;
import com.test.list.views.AmazingListView;

public class ContactsActivity extends Activity
{
	public static AmazingLazyAdapter m_adapter;
	public static String sLOCK = "";

	private AmazingListView _list;
	private EditText _filterText = null;
	private ViewableList _contactList;
	private ContactMngr _contactMngr;// = ContactMngr.getInstance();

	// private Activity _act;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// _act = this;

		setContentView(R.layout.choose_contact_layout);
		_filterText = (EditText) findViewById(R.id.search_box);
		_filterText.addTextChangedListener(filterTextWatcher);

		_contactMngr = ContactMngr.getInstance();
		_contactMngr.setContext(getApplicationContext());
		Controller.getInstance().setContext(getApplicationContext());
		Controller.getInstance().setFilterText(_filterText);

		_list = (AmazingListView) findViewById(R.id.list);
		_list.setPinnedHeaderView(LayoutInflater.from(this).inflate(
				R.layout.item_composer_header, _list, false));

		_contactMngr.loadViewableList();
		_contactList = _contactMngr.getContacts(null);

		m_adapter = new AmazingLazyAdapter(this, _contactList.getViewables());// ,
		m_adapter.refreshAll();

		_list.setAdapter(m_adapter);

		_list.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> a, View view, int position,
					long id)
			{
				Controller.getInstance().hideKeyboard();
				Viewable viewable = m_adapter.getItem(position);
				viewable.onClick();
			}
		});

		_list.setOnItemLongClickListener(new OnItemLongClickListener()
		{
			@Override
			public boolean onItemLongClick(AdapterView<?> a, View v,
					int position, long id)
			{
				Controller.getInstance().hideKeyboard();
				Viewable viewable = m_adapter.getItem(position);
				viewable.onLongClick();
				return false;

			}
		});
	}

	private TextWatcher filterTextWatcher = new TextWatcher()
	{

		public void afterTextChanged(Editable s)
		{
			sLOCK = s.toString();
			m_adapter.getFilter().filter(s.toString());
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after)
		{
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count)
		{

		}

	};

	@Override
	public void onDestroy()
	{
		_list.setAdapter(null);
		_filterText.removeTextChangedListener(filterTextWatcher);
		super.onDestroy();

	}
}